# topictool.pl
# vi: et sw=2
#
# TopicTool script make easier manage topics
# Copyright (C) 2009  Harry Karvonen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# Author: Harry Karvonen <harry.karvonen@gmail.com>
#

#
# Version: 0.1a
#

use strict;

my $version = "0.1a";
my $cmd = "topic-tool";
my $default_parser = "\\|\\|";
my %parser;

my $debug = 1;

Xchat::register("Topic tool", $version, "Help manage topics");

Xchat::hook_command("${cmd}", "command_callback");

Xchat::hook_server("TOPIC", "hook_topic");

sub strip_space {
  my $str = shift;

#  Xchat::print("\$str = $str");
  # being spaces
  $str =~ s/^\s*(.+)$/\1/;

#  Xchat::print("\$str = $str");
  # end spaces
  while ($str =~ /\s$/) {
    chop $str;
  }

#  Xchat::print("\$str = $str");
  return $str;
}

# syntax: $[cmd} action <data> <channel>
sub command_callback {
  my $action = lc $_[0][1];
  my $data = "";
  my $channel = lc Xchat::get_info("channel");
  my $msg_channel = $channel;
  my $topic = "";

  # help
  if ($action eq "help") {
    &show_help();
    return Xchat::EAT_ALL;
  # show there is no data
  } elsif ($action eq "show") {
    # If there is given channel
    if ($_[0][2] ne "") {
      $channel = lc $_[0][2];
      Xchat::set_context($channel);
    }

    $topic = Xchat::get_info("topic");

    if ($debug > 0) {
      Xchat::print("DEBUG\t\$parser{$channel} == $parser{$channel}",
						$msg_channel);
    }

    # use default parser
    if ($parser{$channel} eq "") {
      Xchat::print("Using default parser $default_parser", $msg_channel);
      $parser{$channel} = $default_parser;
    }

    my @split = split($parser{$channel}, $topic);

    my $i = 1;
    foreach my $s (@split) {
#      if ($s =~ /^\s*(.*)\s*$/) {
#	$s = $1;
#      }

      $s = strip_space $s;

      Xchat::print("$i: $s", $msg_channel);
      $i++;
    }
  # add
  } elsif ($action eq "add") {
    if ($_[0][2] eq "") {
      &show_help("add");
      return Xchat::EAT_ALL;
    }

    $data = $_[1][2];

    my $p = $parser{$channel};

    if ($debug > 0) {
      Xchat::print("DEBUG\tUnfixed \$p = $p", $msg_channel);
    }

    $p =~  s/\\//g;

    if ($debug > 0) {
      Xchat::print("DEBUG\tFixed \$p = $p", $msg_channel);
    }

    $topic = Xchat::get_info("topic") . " $p " . $data;

    if ($debug > 0) {
      Xchat::print("DEBUG\tTOPIC: $topic", $msg_channel);
    }

    Xchat::command("TOPIC $topic");
  # del
  } elsif ($action eq "del") {
    if ($_[0][2] eq "") {
      &show_help("del");
      return Xchat::EAT_ALL;
    }

    $data = $_[0][2];

    if ($data =~ /^[^0-9-]+/) {
      &show_help("del");
      return Xchat::EAT_ALL;
    }

    if ($_[0][3] ne "") {
      $channel = $_[0][3];
      Xchat::set_context($channel);
    }

    $topic = Xchat::get_info("topic");
    
    my @split = split($parser{$channel}, $topic);
    my $i = 1;

    $topic = "";

    my $p = $parser{$channel};
    $p =~  s/\\//g;

    foreach my $s (@split) {
      if ($data != $i) {
	$topic .= strip_space $s;
      }

      if ($data != $i) {
	$topic .= " $p ";
      }

      $i++;
    }

    $topic =~ s/^(.+) $parser{$channel} $/\1/;

    if ($debug > 0) {
      Xchat::print("DEBUG\tTOPIC: $topic", $msg_channel);
    }

    Xchat::command("TOPIC $topic");
  } elsif ($action eq "change") {
    if ($_[0][2] eq "" && $_[0][3] eq "") {
      &show_help("change");
      return Xchat::EAT_ALL;
    } 

    my $number = $_[0][2];

    if ($number =~ /^\D+/) {
      &show_help("change");
      return Xchat::EAT_ALL;
    }

    $data = $_[0][3];

    if ($_[0][4] ne "") {
      $channel = lc $_[0][4];
      Xchat::set_context($channel);
    }

    # FIXME Tee loppuun
    # FIXME Tee loppuun
    # FIXME Tee loppuun

    $topic =~ s/^(.+) $parser{$channel} $/\1/;

    if ($debug > 0) {
      Xchat::print("DEBUG\tTOPIC: $topic", $msg_channel);
    }

    Xchat::command("TOPIC $topic");

  } elsif ($action eq "parser") {
    if ($_[0][2] eq "") {
      &show_help("parser");
      return Xchat::EAT_ALL;
    }

    $data = $_[0][2];

    if ($_[0][3] ne "") {
      $channel = lc $_[0][3];
    }

    $parser{$channel} = $data;

    Xchat::print("Channel $channel parser is now $data", $msg_channel);
  }

  return Xchat::EAT_ALL;
}

sub hook_topic {

  return Xchat::EAT_NONE;
}

sub show_help
{
  Xchat::print("Topic tool help");
}

# FIXME Asetusten tallennus ja lataus
# FIXME Asetusten tallennus ja lataus
# FIXME Asetusten tallennus ja lataus

Xchat::print("Topic tools v${version} loaded");
